package com.lemon.mp3player;

import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mp;

    Button playButton;
    Button pauseButton;
    Button stopButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mp = MediaPlayer.create(MainActivity.this, R.raw.music);

        playButton = findViewById(R.id.button1);
        pauseButton = findViewById(R.id.button2);
        stopButton = findViewById(R.id.button3);

    }

    @Override
    protected void onPause() {
        mp.pause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mp.start();
    }

    private void play(){
        try{
            mp.reset();
            mp=MediaPlayer.create(MainActivity.this, R.raw.music);//重新设置要播放的音频
            mp.start();//开始播放
            playButton.setEnabled(false);
            pauseButton.setEnabled(true);
            stopButton.setEnabled(true);
        }catch(Exception e){
            e.printStackTrace();//输出异常信息
        }
    }

    public void onClickButton(View v) {
        switch (v.getId())
        {
            case R.id.button1:
                play();
                break;
            case R.id.button2:

                break;
            case R.id.button3:

                break;
        }
    }
}
